package com.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.service.UserService;
import com.service.Impl.UserServiceImpl;
import com.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

@Controller
@Scope("prototype")
public class UserAction {
	@Autowired
	private UserService service;
	private String userEmail;
	private String password;
	private String password1;
	private User u;
	public User getU() {
		return u;
	}
	public void setU(User u) {
		this.u = u;
	}
	public String getUserEmail() {
		return userEmail;
	}
	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}
	public String getPassword1() {
		return password1;
	}
	public void setPassword1(String password1) {
		this.password1 = password1;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String login() throws ServletException, IOException{
		User u=service.select(userEmail, password);
		if(u!=null){
			HttpSession session = ServletActionContext.getRequest().getSession();
			session.setAttribute("u",u);
			return "loginSuccess";
		}else{
			return "loginError";
		}	
	}
	
	public String register(){
		if((u.getPassword()).equals(password1)){
			service.insert(u);
			return "registerSuccess";
		}else{
			return "registerError";
		}
	}
}
